<?php
/**
*
* 功能描述（用户控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlUser extends CtrlBase
{
	public $paramCheck = array(
		"checkmobile" => array(
			"mobile" => array("GET", "must", "trim", "mobile"),
		),
		"register" => array(
			'type' => array('POST', 'must', 'intval', 'positive'),
			"mobile" => array("POST", "must", "trim", "mobile"),
			"password" => array("POST", "must", "trim", "notempty"),
			"authcode" => array("POST", "must", "intval", "positive"),
			"mobile_auth" => array("SESSION", "must", "", ""),
			'com_name' => array("POST", "", "trim", "notempty"),
			'com_tel' => array("POST", "", "trim", "tel"),
		),
		"login" => array(
			"before_url" => array("POST", "", "trim", "notempty"),
			"username" => array("POST", "must", "trim", "notempty"),
			"password" => array("POST", "must", "trim", "notempty"),
			"keep" => array("POST", "0", "intval", "bool"),
		),
		"deladdress" => array(
			"address_id" => array("GET", "must", "intval", "notnegative"),
		),
		"addaddress" => array(
			"province" => array("POST", "must", "intval", "positive"),
			"city" => array("POST", "must", "intval", "positive"),
			"district" => array("POST", "must", "intval", "positive"),
			"area" => array("POST", "must", "intval", "notnegative"),
			"address" => array("POST", "must", "trim", "notempty"),
			"consignee" => array("POST", "must", "trim", "notempty"),
			"mobile_from" => array("POST", "must", "trim", "notempty"),
			"mobile_to" => array("POST", "must", "trim", "notempty"),
			"address_id" => array("POST", "must", "intval", "notnegative"),
		),
		"modinfo" => array(
			'birthdayYear' => array("POST", "must", "intval", "positive"),
			'birthdayMonth' => array("POST", "must", "intval", "positive"),
			'birthdayDay' => array("POST", "must", "intval", "positive"),
			'sex' => array("POST", "must", "intval", "notnegative"),
			'username' =>  array("POST", "must", "trim", "notempty"),
			'email' => array("POST", "must", "trim", "email"),
			'mobile' => array("POST", "must", "trim", "mobile"),
			'truename' => array("POST", "must", "trim", ""),
			'com_name' => array("POST", "must", "trim", "notempty"),
			'com_tel' => array("POST", "must", "trim", "tel"),
		),

		'docharge' => array(
			'type' => array('POST', 'must', 'intval', 'positive'),
			'amount' => array('POST', 'must', 'floatval', 'positive'),
		),

		'chpwd' => array(
			'old_password' => array('POST', 'must', 'trim', 'notempty'),
			'new_password' => array('POST', 'must', 'trim', 'notempty'),
		),
	);

	public $perms = array(
		'index' => array(1, 255, "index.php?action=user.tologin"),
		'info' => array(1, 255, "index.php?action=user.tologin"),
		'modinfo' => array(1, 255, "index.php?action=user.tologin"),
		'orderlist' => array(1, 255, "index.php?action=user.tologin"),
		'address' => array(1, 255, "index.php?action=user.tologin"),
		'addaddress' => array(1, 255, "index.php?action=user.tologin"),
		'lvmsg' => array(1, 255, "index.php?action=user.tologin"),
		'getmoney' => array(1, 255, "index.php?action=user.tologin"),
		'charge' => array(1, 255, "index.php?action=user.tologin"),
		'docharge' => array(1, 255, "index.php?action=user.tologin"),
		'chpwd' => array(1, 255, "index.php?action=user.tologin"),
	);


	function Index()
	{
		$obj = $this->GetModel("user");
		$data = $obj->GetAccInfo($this->user->userid);
		if(!$data)
			return RT_ATTACK;
		$this->assign("data", $data);
		return RT_TPL;
	}

	function ChPwd()
	{
		$oldpwd = $this->params['old_password'];
		$newpwd = $this->params['new_password'];
		if($oldpwd == $newpwd)
		{
			echo PWD_SAME;
			return RT_NONE;
		}
		$obj = $this->GetModel("user");
		$cnt = $obj->SetPwd($this->user->userid, $newpwd, $oldpwd);
		if($cnt == 0)
			echo '原密码错误';
		else
			echo '0';
		return RT_NONE;
	}

	function ToLogin()
	{
		$before = '';
		if(array_key_exists("before_login", $_SESSION))
		{
			$before = $_SESSION['before_login'];
			unset($_SESSION['before_login']);
		}
		$this->assign('before', $before);
		return RT_TPL;
	}

	function GetMoney()
	{
		$obj = $this->GetModel("user");
		$data = $obj->GetAccInfo($this->user->userid);
		echo $data['money'];
		return RT_NONE;
	}

	function Info()
	{
		$obj = $this->GetModel("user");
		$data = $obj->GetInfo($this->user->userid, 1);
		if(!$data)
			return RT_ATTACK;
		$this->assign("data", $data);
		$this->assign("usertype", $this->user->usertype);
		return RT_TPL;
	}

	function ModInfo()
	{
		$mobile = $this->params['mobile'];
		$obj = $this->GetModel("user");
		$data = array(
			'bYear' => $this->params['birthdayYear'],
			'bMonth' => $this->params['birthdayMonth'],
			'bDay' => $this->params['birthdayDay'],
			'sex' => $this->params['sex'],
			'name' => $this->params['username'],
			'email' => $this->params['email'],
			'mobile' => $this->params['mobile'],
			'truename' => $this->params['truename'],
			'com_name' => $this->params['com_name'],
			'com_tel' => $this->params['com_tel'],
		);
		$obj->SaveInfo($this->user->userid, $data);
		$_SESSION['user'] = array(
			"id" => $this->user->userid,
			"name" => $data['name'],
			"com_name" => $data['com_name'],
			"logintime" => time(0),
			"mobile" => $data['mobile'],
			"level" => $_SESSION['user']['level'],
			"usertype" => $_SESSION['user']['usertype'],
			'keep' => $_SESSION['user']['keep'],
		);
		jump_to("index.php?action=user.info");
		return RT_NONE;
	}

	function CheckMobile()
	{
		$mobile = $this->params['mobile'];
		$obj = $this->GetModel("user");
		$data = $obj->GetByMobile($mobile);
		if(!$data)
			echo 0;
		else
			echo 1;
		return RT_NONE;
	}

	function ToRegister()
	{
		return RT_TPL;
	}

	function Register()
	{
		$type = $this->params['type'];
		if($type != 1 && $type != 2)
			return RT_ATTACK;
		$mobile = $this->params['mobile'];
		$obj = $this->GetModel("mobile");
		if($obj->CheckAuthcode($mobile, $this->params['authcode'], $this->params['mobile_auth'], 1) != 0)
			return RT_ATTACK;
		unset($_SESSION['mobile_auth']);	//必须清除
		$obj = $this->GetModel("user");
		$data = array(
			'com_name' => $this->params['com_name'],
			'com_tel' => $this->params['com_tel'],
		);
		$ret = $obj->CreateUser($type, $mobile, $this->params['password'], $data);
		if($ret > 0)
		{
			$_SESSION['user'] = array(
				"id" => $ret,
				"name" => $mobile,
				"com_name" => '',
				"logintime" => time(0),
				"mobile" => $mobile,
				"level" => 1,
				"usertype" => $type,
				'keep' => 0,
			);
			$this->LoadInfo();
			$msg = "恭喜您注册成功";
		}
		else
			$msg = "注册失败，请核实注册信息";
		$this->assign("msg", $msg);
		return RT_TPL;
	}

	function Login()
	{
		$obj = $this->GetModel("user");
		$data = $obj->Login($this->params['username'], $this->params['password']);
		if(!$data)
			$msg = "用户不存在或者密码错误";
		else if($data['level'] <= 0)
			$msg = "账号封停";
		else
		{
			$_SESSION['user'] = array(
				"id" => $data['id'],
				"name" => $data['name'],
				"com_name" => $data['com_name'],
				"logintime" => time(0),
				"mobile" => $data['mobile'],
				"level" => $data['level'],
				"usertype" => $data['type'],
				"keep" => $this->params['keep'],
			);
			$this->LoadInfo();
			$msg = "登录成功";
			$tourl = $this->params['before_url'];
			if(!empty($tourl))
				jump_to($tourl);
		}
		$this->assign("msg", $msg);
		return RT_TPL;
	}

	function Logout()
	{
		//权限已经保证已经登录
		unset($_SESSION['user']);
		$this->LoadInfo();
		return RT_TPL;
	}

	function OrderList()
	{
		return RT_TPL;
	}

	function Address()
	{
		$obj = $this->GetModel("Address");
		$data = $obj->GetList($this->user->userid);
		$this->assign("address", $data);
		$this->assign("mobile", $this->user->mobile);
		return RT_TPL;
	}

	function DelAddress()
	{
		$addId = $this->params["address_id"];
		$obj = $this->GetModel("Address");
		$cnt = $obj->DelAddress($this->user->userid, $addId);
		if($cnt > 0)
			echo '0';
		else
			echo '2';
		return RT_NONE;
	}

	function AddAddress()
	{
		$data = array(
			"province" => $this->params["province"],
			"city" => $this->params["city"],
			"district" => $this->params["district"],
			"area" => $this->params["area"],
			"address" => $this->params["address"],
			"consignee" => $this->params["consignee"],
			"mobile_to" => $this->params["mobile_to"],
			"mobile_from" => $this->params["mobile_from"],
		);

		$addId = $this->params["address_id"];
		$obj = $this->GetModel("Address");
		if($addId == 0)
		{
			$data['id'] = "NULL";
			$data['userid'] = $this->user->userid;
			$ret = $obj->AddAddress($data);
			if($ret <= 0)
			{
				$this->assign("msg", "添加失败");
				return RT_MSG;
			}
		}
		else
			$ret = $obj->ModAddress($this->user->userid, $data, $addId);
		jump_to("index.php?action=user.address");
	}

	function ModPwd()
	{
		return RT_TPL;
	}

	function LvMsg()
	{
		return RT_TPL;
	}

	function Charge()
	{
		return RT_TPL;
	}

	function CheckLogin()
	{
		if($this->user->logined)
			echo "0";
		else
			echo "1";
		return RT_NONE;
	}

	function DoCharge()
	{
		//var_dump($this->params);
		$type = $this->params['type'];
		$amount = $this->params['amount'];
		$info = USER_CHARGE;
		global $web_root;
		$data = array(
			'trade_type' => 1,
			'type' => $type,
			'amount' => $amount,
			'id' => 0,
			'user_id' => $this->user->userid,
			'info' => $info,
		);
		$obj = $this->GetModel('trade');
		$id = $obj->NewItem($data);
		switch($type)
		{
		case 1:
			DisplayZFB($id, $this->params['amount'], $info, $web_root['web'].'index.php?action=user.charge');
			break;
		}
		return RT_NONE;
	}
}

